package com.yiwei.jmx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.MyQuataAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.model.UserChannelAuthModel;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.event.ChangeHomePageEvent;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyQuataActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    @BindView(R.id.list)
    RecyclerView list;
    private Unbinder unbiner;
    private MyQuataAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_quata);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的额度");
        txtRight.setVisibility(View.VISIBLE);
        txtRight.setText("提智信分");
        adapter = new MyQuataAdapter(this);
        adapter.setOnChooseListener(new MyQuataAdapter.OnChooseListener() {
            @Override
            public void onSuccess(UserChannelAuthModel.DataBean.ChannelsBean item) {
                Intent intent = new Intent(MyQuataActivity.this, BorrowStepActivity.class);
                intent.putExtra("company_id", item.company_id);
                intent.putExtra("channel_id", item.channel_id);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }
        });
        list.setAdapter(adapter);
        getUserChannelAuth();
    }

    private void getUserChannelAuth() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        Api.api_service.getUserChannelAuth().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.update(result.data.channels);
                    txtMoney.setText(result.data.money_sum);
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }



    @OnClick({R.id.btn_back, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                EventBus.getDefault().post(new ChangeHomePageEvent(2));
                finish();
                break;
        }
    }
}
